/*
 * common.h
 *
 *  Created on: May 3, 2011
 *      Author: tvale, dmourao
 */

#ifndef COMMON_H_
#define COMMON_H_

#include <mpi.h>

/*
 * MPI tags/message types.
 *      GET:        when client issues get();
 *      GET_ANSWER: server response to client's get();
 *      SET:        when client issues set(int);
 *      WRITE:      when server propagates a client's set(int);
 *      INQUIRE:    when server needs to decide about another server's state;
 *      ACK:        reply from an inquiry.
 *      FINISHED:   client terminated.
 */
typedef enum msg_type {
  MSG_GET_CLT  = 0,
  MSG_GET_SRV  = 1,
  MSG_SET      = 2,
  MSG_WRITE    = 3,
  MSG_INQUIRE  = 4,
  MSG_ACK      = 5,
  MSG_STOP_CLT = 6,
  MSG_STOP_SRV = 7
} msg_type_t;

/*
 * Custom MPI datatypes.
 */
MPI_Datatype MPI_CLOCK;
MPI_Datatype MPI_WRITE;
MPI_Datatype MPI_MESSAGE;

/*
 * Data structures.
 *      clock:   lamport clock;
 *      write:   new variable value;
 *      message: rough polimorphism, generic message in which payload depends
 *               on type.
 */
typedef struct clock {
  int timestamp;
  int rank;
} clock_t;

typedef struct write {
  int     value;
  clock_t clock;
} write_t;

typedef struct msg {
  msg_type_t type;
  union {
    write_t write;
    clock_t clock;
    int     value;
  } payload;
} msg_t;

#endif /* COMMON_H_ */
